package com.manager.plat.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IConfigRouterDao;
import com.manager.plat.dao.ILimitUserConsumDao;
import com.manager.plat.vo.LimitUserConsumVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class LimitUserConsumAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2647318582337354706L;

	private static Logger logger = Logger.getLogger(LimitUserConsumAction.class);
	
	private ILimitUserConsumDao limitUserConsumDao;			//用户总量限制数据接口
	private IConfigRouterDao configRouterDao;			//路由信息数据接口
	private List<LimitUserConsumVo> limitUserConsumVoList;	//总户总量限制信息列表
	private List<String> consumSpnumberList;				//自消费业务长号码列表
	private LimitUserConsumVo limitUserConsumVo;			//用户总量信息Vo
	private String actionPath;					//操作成功跳转路径
	
	public String getLimitUserConsumList(){
		limitUserConsumVoList = limitUserConsumDao.getLimitUserConsumList();
		return SUCCESS;
	}
	
	public String addLimitUserConsum(){
		limitUserConsumVo = null;
		consumSpnumberList = configRouterDao.getSpnumberConsumList();
		return SUCCESS;
	}
	
	public String editLimitUserConsum(){
		if(limitUserConsumVo.getId() != 0){
			limitUserConsumVo = limitUserConsumDao.getLimitUserConsumById(limitUserConsumVo.getId());
		}
		consumSpnumberList = configRouterDao.getSpnumberConsumList();
		return SUCCESS;
	}
	
	public String updateLimitUserConsum(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		
		limitUserConsumVo.setAdd_user(platUser.getId());
		if(limitUserConsumVo.getId() == 0){
			//判断用户总量限制表是否已经存在，已经存在的话就不能再次新增
			int count = limitUserConsumDao.insertLimitUserConsum(limitUserConsumVo);
			
			if(count > 0){
				logger.info(String.format("[%s]添加用户总量信息成功", platUser.getLogin_username()));
			}else{
				logger.info(String.format("[%s]添加用户总量信息失败", platUser.getLogin_username()));
			}
		}else {
			int count = limitUserConsumDao.updateLimitUserConsum(limitUserConsumVo);
			if(count > 0){
				logger.info(String.format("[%s]更新用户总量信息成功", platUser.getLogin_username()));
			}else{
				logger.info(String.format("[%s]更新用户总量信息失败", platUser.getLogin_username()));
			}
		}
		
		actionPath = "limitUserConsum/getLimitUserConsumList.action";
		return SUCCESS;
	}
	
	public String deleteLimitUserConsum(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		
		int count = limitUserConsumDao.deleteLimitUserConsum(limitUserConsumVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除用户总量信息成功", platUser.getLogin_username()));
		}else{
			logger.info(String.format("[%s]删除用户总量信息失败", platUser.getLogin_username()));
		}
		
		actionPath = "limitUserConsum/getLimitUserConsumList.action";
		return SUCCESS;
	}
	
	public ILimitUserConsumDao getLimitUserConsumDao() {
		return limitUserConsumDao;
	}

	public void setLimitUserConsumDao(ILimitUserConsumDao limitUserConsumDao) {
		this.limitUserConsumDao = limitUserConsumDao;
	}

	public IConfigRouterDao getConfigRouterDao() {
		return configRouterDao;
	}

	public void setConfigRouterDao(IConfigRouterDao configRouterDao) {
		this.configRouterDao = configRouterDao;
	}

	public List<LimitUserConsumVo> getLimitUserConsumVoList() {
		return limitUserConsumVoList;
	}

	public void setLimitUserConsumVoList(List<LimitUserConsumVo> limitUserConsumVoList) {
		this.limitUserConsumVoList = limitUserConsumVoList;
	}

	public LimitUserConsumVo getLimitUserConsumVo() {
		return limitUserConsumVo;
	}

	public void setLimitUserConsumVo(LimitUserConsumVo limitUserConsumVo) {
		this.limitUserConsumVo = limitUserConsumVo;
	}

	public List<String> getConsumSpnumberList() {
		return consumSpnumberList;
	}

	public void setConsumSpnumberList(List<String> consumSpnumberList) {
		this.consumSpnumberList = consumSpnumberList;
	}

	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}
